<!--
 * @Description: 
 * @Author: mmzhang
 * @Date: 2022-01-17 15:04:02
 * @LastEditTime: 2022-01-17 16:33:21
-->
<template>
  <div class="sys-channel">
    <Button type="primary" @click="addChannel" v-if="unlocking('0204020100')"
      style="margin-bottom:20px;">新增配置</Button>
    <dt-grid ref="table" :onFetch='onFetch' />
    <!--新增修改-->
    <dt-modal-edit ref="edit" title="渠道配置" @ok="onOk"></dt-modal-edit>
  </div>
</template>
<script>
  import sysColumn from './sysColumns'
  import channelMgr from '@/biz/channel/channel'
  import ParamsChannel from './editChannel'
  export default {
    data() {
      return {
        channelInfo: {
          channelType: '',
          channelName: '',
          validate: '',
        },
        operateType: 0 // 0新增 1修改
      }
    },
    mounted() {
      this.$refs.table.init(this, new sysColumn(this))
      this.$refs.table.fetch()
    },
    methods: {
      addChannel() {
        this.operateType = 0
        this.channelInfo = {
          channelType: '',
          channelName: '',
          validate: '',
        }
        this.$refs.edit.show(new ParamsChannel(this).options(), this.channelInfo)
      },
      editMess(row) {
        this.operateType = 1
        this.channelInfo = { ...row }
        this.channelInfo.channelType = this.channelInfo.channelType.toString()
        this.channelInfo.validate = this.channelInfo.validate.toString()

        this.$refs.edit.show(new ParamsChannel(this).options(), this.channelInfo)
      },
      channelDel(row) {
        let params = {
          channelId: row.channelId,
        }

        this.$Modal.confirm({
          title: '删除渠道',
          content: '<p>是否删除该渠道？</p>',
          onOk: () => {
            channelMgr.channelDelete(params).then((res) => {
              this.$Message.success('渠道删除成功。')
              this.$refs.table.fetch()
            })
          },
          onCancel: () => {
            this.$Message.info('你取消了删除。')
          },
        })
      },
      onFetch() {
        let params = {
          page: 1,
          rows: 500,
        }
        return channelMgr.channelList(params).then((res) => {
          return res.list
        })
      },
      onOk() {
        if (this.operateType == 0) {
          channelMgr
            .channelInsert(this.channelInfo)
            .then((res) => {
              this.$Message.success('渠道新增成功。')
              this.$refs.edit.hide()
              this.$refs.table.fetch()
            })
            .catch((err) => {})
        } else {
          channelMgr
            .channelUpdate(this.channelInfo)
            .then((res) => {
              this.$Message.success('渠道编辑成功。')
              this.$refs.edit.hide()
              this.$refs.table.fetch()
            })
            .catch((err) => {})
        }
      }
    }
  }
</script>